package com.mdinic.blogovi.front.page.blogpost;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import com.mdinic.blogovi.front.common.BDateTimeLabel;
import com.mdinic.blogovi.front.common.OddRowIndicator;
import com.mdinic.blogovi.model.BlogPost;

public class BlogPostPersonPanel extends Panel {
    private static final long serialVersionUID = 1L;

    public static class Config implements Serializable {
        private static final long serialVersionUID = 1L;

        private final boolean voteUpVisible;
        private final boolean voteDownVisible;
        private final boolean clickVisible;
        private final boolean viewVisible;
        private final boolean postVisible;
        private final boolean commentVisible;

        public Config(boolean voteUpVisible, boolean voteDownVisible, boolean clickVisible, boolean viewVisible,
                boolean postVisible, boolean commentVisible) {
            super();
            this.voteUpVisible = voteUpVisible;
            this.voteDownVisible = voteDownVisible;
            this.clickVisible = clickVisible;
            this.viewVisible = viewVisible;
            this.postVisible = postVisible;
            this.commentVisible = commentVisible;
        }

        public boolean isVoteUpVisible() {
            return voteUpVisible;
        }

        public boolean isVoteDownVisible() {
            return voteDownVisible;
        }

        public boolean isClickVisible() {
            return clickVisible;
        }

        public boolean isViewVisible() {
            return viewVisible;
        }

        public boolean isPostVisible() {
            return postVisible;
        }

        public boolean isCommentVisible() {
            return commentVisible;
        }

    }

    public BlogPostPersonPanel(String id, List<BlogPost> posts, final Config config) {
        super(id);

        add(new WebMarkupContainer("hvoteup").setVisible(config.isVoteUpVisible()));
        add(new WebMarkupContainer("hvotedown").setVisible(config.isVoteDownVisible()));
        add(new WebMarkupContainer("hclick").setVisible(config.isClickVisible()));
        add(new WebMarkupContainer("hview").setVisible(config.isViewVisible()));
        add(new WebMarkupContainer("hdate").setVisible(config.isPostVisible()));
        add(new WebMarkupContainer("hcomment").setVisible(config.isCommentVisible()));

        ListView<BlogPost> list = new ListView<BlogPost>("list", posts) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<BlogPost> item) {

                item.add(new Label("number", item.getIndex() + 1));

                item.add(new Label("vote_up_nr", new PropertyModel<Integer>(item.getModel(), "voteUp"))
                        .setVisible(config.isVoteUpVisible()));
                item.add(new Label("vote_down_nr", new PropertyModel<Integer>(item.getModel(), "voteDown"))
                        .setVisible(config.isVoteDownVisible()));

                PageParameters parameters = new PageParameters();
                parameters.add(BlogPostViewPage.URL_PARAM_NAME, item.getModelObject().getId());
                BookmarkablePageLink<BlogPostViewPage> blogPostLink = new BookmarkablePageLink<BlogPostViewPage>(
                        "blog_post_link", BlogPostViewPage.class, parameters);

                blogPostLink.add(new Label(BlogPost.HEADLINE, item.getModelObject().getHeadline()));

                item.add(blogPostLink);

                item.add(new Label(BlogPost.CLICK, item.getModelObject().getClick()).setVisible(config.isClickVisible()));
                item.add(new Label(BlogPost.VIEW, item.getModelObject().getView()).setVisible(config.isViewVisible()));

                item.add(new BDateTimeLabel(BlogPost.POSTED, new PropertyModel<Date>(item, "modelObject.posted"))
                        .setVisible(config.isPostVisible()));
                item.add(new Label(BlogPost.COMMENT, item.getModelObject().getComment()).setVisible(config
                        .isCommentVisible()));

                item.add(new OddRowIndicator(item));

            }
        };

        add(list);
    }
}
